<?php 
class ModelSettingSetting extends Model {

    private $insert_query;
    private $update_query;
    private $delete_query;
    private $app_query;
    private $key_query;

    public function __construct($registry){
        parent::__construct($registry);

        $this->update_query = $this->pdo->prepare("UPDATE " . $this->db_prefix . "setting SET 
            s_value     = :value,
            serialized      = :serialized 
            WHERE   app_id      = :app_id
            AND     s_key       = :key
            AND     store_id    = :store_id");

        $this->insert_query = $this->pdo->prepare("INSERT INTO " . $this->db_prefix . "setting (
            store_id, app_id, s_key, s_value, serialized
        ) VALUES (
            :store_id, :app_id, :key, :value, :serialized
        )");

        $this->key_query = $this->pdo->prepare("SELECT * FROM " . $this->db_prefix . "setting 
            WHERE store_id  = :store_id 
            AND app_id = :app_id 
            AND s_key = :key");

        $this->app_query = $this->pdo->prepare("SELECT * FROM " . $this->db_prefix . "setting 
            WHERE store_id  = :store_id 
            AND app_id       = :app_id");

        $this->delete_query = $this->pdo->prepare("DELETE FROM " . $this->db_prefix . "setting 
            WHERE   'store_id'    = :store_id
            AND     'app_id'       = :app_id");

    }

    public function getSetting($app_id, $store_id = 0) {

        $data = array(); 

        $this->app_query->execute(array(
            ':store_id'  => $store_id,
            ':app_id'     => $app_id
        ));

        $setting = $this->app_query->fetchAll(PDO::FETCH_ASSOC); 

        foreach ($setting as $setting) {
            if (!$setting['serialized']) {
                $data[$setting['s_key']] = $setting['s_value'];
            } else {
                $data[$setting['s_key']] = unserialize($setting['s_value']);
            }
        }

        return $data;
    }

    public function editSetting($app_id, $data, $store_id = 0) {
        foreach ($data as $key => $value) {

            $serial = 0;
            if (is_array($value)) {
                $value = serialize($value);
                $serial = 1;
            }

            $this->key_query->execute(array(
                ':store_id'     => $store_id, 
                ':app_id'        => $app_id, 
                ':key'          => $key, 
            ));

            if($this->key_query->rowCount() == 0) {

                $this->insert_query->execute(array(
                    ':store_id'     => $store_id, 
                    ':app_id'        => $app_id, 
                    ':key'          => $key, 
                    ':value'        => $value,
                    ':serialized'   => $serial
                ));
            } else {
                $this->update_query->execute(array(    
                    ':value'        => $value,
                    ':app_id'        => $app_id,   
                    ':key'          => $key,
                    ':store_id'     => $store_id,
                    ':serialized'   => $serial));
            }
        }
    }

    public function editSettingValue($app_id = '', $key = '', $value = '', $store_id = 0) {

        if (!is_array($value)) {
            $this->update_query->execute(array(
                ':value'        => $value,
                ':app_id'        => $app_id,   
                ':key'          => $key,
                ':store_id'     => $store_id,
                ':serialized'   => '0'));
        } else {
            $this->update_query->execute(array(
                ':value'        => serialize($value),
                ':app_id'        => $app_id,   
                ':key'          => $key,
                ':store_id'     => $store_id,
                ':serialized'   => '1'));
        }
    }

    public function deleteSetting($app_id, $store_id = 0) {
        $this->delete_query->execute(array(
            ':store_id' => $store_id,
            ':app_id'    => $app_id
        ));
    }	
}
?>
